package nciae.db.controller;

import nciae.db.common.CommonResult;
import nciae.db.entity.Device;
import nciae.db.entity.DeviceData;
import nciae.db.service.DeviceDataService;
import nciae.db.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/devicedata")
public class DeviceDataController {

    @Autowired
    private DeviceDataService deviceDataService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public CommonResult add(DeviceData deviceData){
        return deviceDataService.add(deviceData);
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public CommonResult edit(DeviceData deviceData){
        return deviceDataService.edit(deviceData);
    }

    @RequestMapping(value = "/search",method = RequestMethod.POST)
    public CommonResult search(Long deviceId, Integer page, Integer size){
        return deviceDataService.listByPage(deviceId,page,size);
    }

    @RequestMapping(value = "/get",method = RequestMethod.POST)
    public CommonResult getDeviceDataById(Integer id){
        CommonResult rs = new CommonResult();
        try{
            DeviceData deviceData = deviceDataService.getDeviceDataById(id);
            rs.setCode(0);
            rs.setMsg("获取设备成功");
            rs.setData(deviceData);
            return rs;
        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }

    @RequestMapping(value = "/del",method = RequestMethod.POST)
    public CommonResult delete(Integer id){
        return deviceDataService.del(id);
    }
}
